package com.example.yaohao.test_cz;

import com.sun.org.apache.bcel.internal.generic.NEW;

import java.util.*;

/**
 * 说明:
 *
 * @Author: chengzi
 * @Date: 2018/8/22  15:17
 */
public class YaoHao {

    public static void main(String[] args) {
        YaoHao yaoHao = new YaoHao();
        List<User> userList = new ArrayList<>();
        User user;
        for (int i = 0; i < 5000000 ; i++) {
            user = new User();
            user.setUserId(i);
            user.setFactor(yaoHao.yaoHao(10));
            userList.add(user);
        }
        List<Integer> numbers = yaoHao.getNumbers(userList, 1000);
        System.out.println("中奖选手为: ");
        for (Integer hao : numbers) {
            System.out.println(hao);
        }
    }


    public List<Integer> getNumbers(List<User> userList, Integer number){
        List<Integer> list = new ArrayList<>();
        Map<Integer,Integer> map = new HashMap<>();

        int x = 0;
        //摇号逻辑
        for (int i = 0; i < userList.size(); i++) {
            for(int j = 0; j < userList.get(i).getFactor(); j++){
                map.put(++x,userList.get(i).getUserId());
            }
        }
        for(int i = 0; i < number; i++){
            Integer hao = yaoHao(x);
            while (list.contains(map.get(hao))){
                hao = yaoHao(x);
            }
            list.add(map.get(hao));
        }
        return list;
    }

    private Integer yaoHao(int x){
        return new Random().nextInt(x - 1) + 1;
    }
}
